package cn.net.withub.dataCollector.common.utils;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by yuanjie on 2017/12/13.
 */
public class JdbcUtils {
    public static final String url = "jdbc:mysql://192.1.43.51:3306/xfgl";
    public static final String driverName = "org.gjt.mm.mysql.Driver";
    public static final String user = "root";
    public static final String password = "withub";

    private Logger log = Logger.getLogger(this.getClass());

    public Connection getMsqlConnection() {
        try {
            // 加载驱动程序
            Class.forName(driverName);
            // 连续数据库
            Connection conn = DriverManager.getConnection(url, user, password);

            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //准备执行语句
    public PreparedStatement excuteSql(String sql){
        PreparedStatement pst = null;
        Connection conn = null;
        try {
            conn = getMsqlConnection();
            log.info("获取Connection连接......: "+conn);
            pst = conn.prepareStatement(sql);
            log.info("执行调用......: "+pst);
            pst.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                pst.close();
                log.info("关闭连接......: ");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return pst;
    }
}
